// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) 
// Source File Name:   GutenbergFormatter.java

package presenter;

import java.io.*;
import java.util.ArrayList;
import javax.swing.JFileChooser;

public class GutenbergFormatter
{

    public GutenbergFormatter(File bible)
    {
        try
        {
            formatted = new ArrayList<String>();
            BufferedReader br;
            for(br = new BufferedReader(new FileReader(bible)); br.readLine().equals("Genesis"););
            formatted.add("Genesis");
            int chapter = 1;
            int verse = 1;
            for(String temp = br.readLine(); temp != null; temp = br.readLine())
            {
                String nextVerse = (new StringBuilder()).append("").append(chapter).append(":").append(verse + 1).toString();
                String nextChapter = (new StringBuilder()).append("").append(chapter + 1).append(":1").toString();
                String newBook = "1:1";
                if(temp.contains(nextVerse) || temp.contains(nextChapter) || temp.contains(newBook))
                {
                    if(!temp.startsWith(nextVerse) && !temp.startsWith(nextChapter) && !temp.startsWith(newBook))
                    {
                        if(temp.contains(nextVerse))
                        {
                            String front = temp.substring(0, temp.indexOf(nextVerse));
                            String back = temp.substring(temp.indexOf(nextVerse), temp.length());
                            formatted.add(front);
                            formatted.add(" ");
                            formatted.add(back);
                            verse++;
                            continue;
                        }
                        if(temp.contains(nextChapter))
                        {
                            String front = temp.substring(0, temp.indexOf(nextChapter));
                            String back = temp.substring(temp.indexOf(nextChapter), temp.length());
                            formatted.add(front);
                            formatted.add(back);
                            chapter++;
                            verse = 2;
                        } else
                        {
                            String front = temp.substring(0, temp.indexOf(newBook));
                            String back = temp.substring(temp.indexOf(newBook), temp.length());
                            formatted.add(front);
                            formatted.add(back);
                            chapter = 1;
                            verse = 2;
                        }
                        continue;
                    }
                    if(temp.contains(nextVerse))
                    {
                        verse++;
                        nextVerse = (new StringBuilder()).append("").append(chapter).append(":").append(verse + 1).toString();
                        if(temp.contains(nextVerse))
                        {
                            String front = temp.substring(0, temp.indexOf(nextVerse));
                            String back = temp.substring(temp.indexOf(nextVerse), temp.length());
                            formatted.add(front);
                            formatted.add(" ");
                            temp = back;
                            verse++;
                        }
                    } else
                    if(temp.contains(nextChapter))
                    {
                        chapter++;
                        verse = 2;
                    } else
                    {
                        chapter = 1;
                        verse = 2;
                    }
                    formatted.add(temp);
                } else
                {
                    formatted.add(temp);
                }
            }

            JFileChooser jfc = new JFileChooser(bible);
            int x = jfc.showSaveDialog(null);
            if(x == 0)
            {
                BufferedWriter bw = new BufferedWriter(new FileWriter(jfc.getSelectedFile()));
                for(int i = 0; i < formatted.size(); i++)
                {
                    bw.write((String)formatted.get(i));
                    bw.newLine();
                }

                bw.close();
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    public static void main(String args[])
    {
        new GutenbergFormatter(new File("Bibles/New King James Version.txt"));
    }

    private ArrayList<String> formatted;
}
